package ch.ethz.fcl.metrobuzz.render.techniques.tesselation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLUtessellatorCallbackAdapter;

import ch.ethz.fcl.metrobuzz.render.techniques.colors.MBColorTable;

public class TessCallBack extends GLUtessellatorCallbackAdapter {
	@Override
	public void begin(int type) {
		GL11.glBegin(type);
	}

	@Override
	public void end() {
		GL11.glEnd();
	}

	@Override
	public void vertex(Object vertexData) {
		if (vertexData instanceof double[]) {
			double[] pointer = (double[]) vertexData;
			if (pointer.length == 7)
				GL11.glColor4d(pointer[3], pointer[4], pointer[5], pointer[6]);
			else if (pointer.length == 3) // default light grey color
				MBColorTable.assignColor(MBColorTable.LIGHTGREY, 0.2f);

			GL11.glVertex3d(pointer[0], pointer[1], pointer[2]);
		}

		else if (vertexData instanceof float[]) {
			float[] pointer = (float[]) vertexData;
			if (pointer.length == 7)
				GL11.glColor4f(pointer[3], pointer[4], pointer[5], pointer[6]);
			else if (pointer.length == 3) // default light grey color
				MBColorTable.assignColor(MBColorTable.LIGHTGREY, 0.2f);

			GL11.glVertex3f(pointer[0], pointer[1], pointer[2]);
		}
	}

	public void combine(double[] coords, Object[] data, float[] weight,
			Object[] outData) {
		int length = ((double[]) data[0]).length;
		double[] vertex = new double[length];
		vertex[0] = coords[0];
		vertex[1] = coords[1];
		vertex[2] = coords[2];

		// calculate color for intersect points, 
		// if only vertex info, don't calculate
		if (length == 7)
			for (int i = 3; i < 7; i++)
				for (int j = 0; j < 4; j++) {
					if (data[j] != null)
						vertex[i] += weight[j] * ((double[]) data[j])[i];
				}

		outData[0] = vertex;
	}
}
